package org.timemanager.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Gallery.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;

/**
 * show user guide
 * @author Dereky
 */
public class UserHelpActivity extends Activity
{
	private int[] imageIds = new int[]{
		R.drawable.theme,R.drawable.widget, R.drawable.main1,
		R.drawable.main2, R.drawable.daily, R.drawable.pie,
		R.drawable.copyright
	};

	private ImageSwitcher switcher;
	private int position;
	private GestureDetector gestureDetector;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_help);
		switcher = (ImageSwitcher) findViewById(R.id.switcher);
		switcher.setFactory(new ViewFactory()
		{
			public View makeView()
			{
				ImageView imageView = new ImageView(UserHelpActivity.this);
				imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
				imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
				return imageView;
			}
		});
		switcher.setInAnimation(AnimationUtils.loadAnimation(this,
			android.R.anim.fade_in));
		switcher.setOutAnimation(AnimationUtils.loadAnimation(this,
			android.R.anim.fade_out));
		position=0;
		switcher.setImageResource(imageIds[position]);
		gestureDetector=new GestureDetector(new OnGestureListener() {
			
			public boolean onSingleTapUp(MotionEvent e) {
				// TODO Auto-generated method stub
				return false;
			}
			
			public void onShowPress(MotionEvent e) {
				// TODO Auto-generated method stub
			}
			
			public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
					float distanceY) {
				// TODO Auto-generated method stub
				return false;
			}
			
			public void onLongPress(MotionEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
					float velocityY) {
				if(e1.getRawX()<e2.getRawX()){
					position--;
					if(position<0){
						position=0;
					}else{
						switcher.setImageResource(imageIds[position]);
					}
				}else{
					position++;
					if(position>=imageIds.length){
						position=imageIds.length-1;
					}else{
						switcher.setImageResource(imageIds[position]);
					}
				}
				return false;
			}
			
			public boolean onDown(MotionEvent e) {
				// TODO Auto-generated method stub
				return false;
			}
		});
	}
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return gestureDetector.onTouchEvent(event);
	}
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			Log.e("onKeyUp","onKeyUp");
			Intent intent=new Intent(UserHelpActivity.this,TimeManagerActivity.class);
			startActivity(intent);
		}
		return super.onKeyUp(keyCode, event);
	}
	
	
}